package com.school.mall.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.mall.common.result.R;
import com.school.mall.sys.entity.MallGoodsColour;
import com.school.mall.sys.entity.MallGoodsType;
import com.school.mall.sys.entity.queryVo.MallGoodsColourQueryVo;
import com.school.mall.sys.entity.queryVo.MallGoodsTypeQueryVo;
import com.school.mall.sys.service.MallGoodsColourService;
import com.school.mall.sys.service.MallGoodsTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品类型表 前端控制器
 */
@RestController
@RequestMapping("/sys/mallGoodsColour")
@Api(tags="商品颜色表")
public class MallGoodsColourController {

    @Autowired
    MallGoodsColourService iMallGoodsColourService;

        /**
        * 查询商品颜色表列表
        */
        @PostMapping("queryMallGoodsColourList")
        @ApiOperation("商品颜色表列表")
        public R queryMallGoodsColourList(@RequestBody MallGoodsColourQueryVo queryVo) {

            QueryWrapper<MallGoodsColour> queryWrapper=new QueryWrapper<>();
            Page<MallGoodsColour> page = iMallGoodsColourService.page(queryVo.getPage(), queryWrapper);
            return R.ok().data("dataList",page);
        }

        /**
         * 保存或者更新商品颜色表信息
         */
        @PostMapping("saveOrUpdateMallGoodsColour")
        @ApiOperation("保存或者更新商品颜色表信息")
        public R saveOrUpdateMallGoodsColour(@RequestBody MallGoodsColour entity) {
            boolean b =true;
            if(StringUtils.isEmpty(entity.getId())){
                b= iMallGoodsColourService.save(entity);
            }else{
                b=iMallGoodsColourService.updateById(entity);
            }
            return R.th(b);
        }


        /**
         * 根据id查询商品颜色表信息
         */
        @GetMapping("getByIdMallGoodsColour/{id}")
        @ApiOperation("根据id查询商品颜色表信息")
        public R getByIdMallGoodsColour(@PathVariable("id") String id) {
             MallGoodsColour dataMallGoodsColour=  iMallGoodsColourService.getById(id);
             return R.ok().data("entity",dataMallGoodsColour);

        }
        /**
         * 根据id删除商品颜色表信息
         */
        @DeleteMapping("removeByIdMallGoodsColour/{id}")
        @ApiOperation("根据id删除商品颜色表信息")
        public R removeByIdMallGoodsColour(@PathVariable("id") String id) {
            boolean effect= iMallGoodsColourService.removeById(id);
            return R.th(effect);

        }
        @DeleteMapping("dropByIdsMallGoodsColour/{ids}")
        @ApiOperation("根据id数组多选删除商品颜色表信息")
        public R dropByIdsMallGoodsColour(@PathVariable("ids") String ids){
            List<String> idList = Arrays.asList(ids.split(","));
            boolean b = iMallGoodsColourService.deleteBatchIds(idList);
            return R.th(b);
        }

        @GetMapping("mallGoodsColourList")
        public R mallGoodsColourList(){
            List<MallGoodsColour> list = iMallGoodsColourService.list();
            List<Map<String, String>> collect = list.stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());

            return R.ok().data("dataList",collect);
        }

}

